package site.xuxingcan;

import site.xuxingcan.Model.UserModel;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * Created by xuxingcan on 16-6-27.
 */
public class GUI {
    private JPanel panel1;
    private JList list1;
    private JButton refresh_button;
    private JTextField textField_user_name;
    private JTextField textField_jifen;
    private JButton button_add;
    private JButton button_set;
    private JButton 下庄Button;
    private JCheckBox isBankerCheckBox;
    private JTextField textField1;
    private JRadioButton 开RadioButton;
    private JRadioButton 关RadioButton;
    private JButton 续庄Button;
    private JTextField textField_isbanker;

    public static void initGlobalFontSetting(Font fnt) {
        FontUIResource fontRes = new FontUIResource(fnt);
        for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource)
                UIManager.put(key, fontRes);
        }
    }

    public static void main(String[] args) {
        Font font = new Font("宋体", Font.PLAIN, 15);
        initGlobalFontSetting(font);
        JFrame frame = new JFrame("GUI");
        frame.setContentPane(new GUI().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(1000, 600);
        frame.setVisible(true);
        Main.main(null);
    }

    public GUI() {
        button_add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserModel userModel = new UserModel();
                userModel.userName = textField_user_name.getText();
                userModel.userIntegral = Integer.parseInt(textField_jifen.getText());
                userModel.isBanker = isBankerCheckBox.isSelected();
                FileHelper.getInstance().updateUser(userModel);
                refresh_button.doClick();
            }
        });
        refresh_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ArrayList<String> list = new ArrayList<String>();
                for (UserModel model : FileHelper.getInstance().getUserModelList()) {
                    String msg = model.userName + (model.isBanker ? "(庄家)" : "") + "        " + model.userIntegral;
                    list.add(msg);
                }
                clearList();
                setList(list);
            }
        });
        list1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int index = list1.getSelectedIndex();
                UserModel model = FileHelper.getInstance().getUserModelList().get(index);
                textField_user_name.setText(model.userName);
                textField_jifen.setText(model.userIntegral + "");
                isBankerCheckBox.setSelected(model.isBanker);
            }
        });
    }

    private void setList(ArrayList<String> listKey) {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < listKey.size(); i++) {
            model.addElement(listKey.get(i));
        }
        list1.setModel(model);
    }

    private void clearList() {
        try {
            ((DefaultListModel) list1.getModel()).removeAllElements();
        } catch (Exception e) {

        }
    }
}
